class DoubleNode {
    int data;
    DoubleNode prev;
    DoubleNode next;

    public DoubleNode(int data) {
        this.data = data;
    }
}

class DoubleCircularLinkedList {
    private DoubleNode head;
    private int length;

    public DoubleCircularLinkedList() {
        head = new DoubleNode(0);
        head.prev = head;
        head.next = head;
        length = 0;
    }

    // 新增：获取头节点的公共方法
    public DoubleNode getHead() {
        return head;
    }

    // 头插法
    public void headInsert(int data) {
        DoubleNode newNode = new DoubleNode(data);
        newNode.next = head.next;
        newNode.prev = head;
        head.next.prev = newNode;
        head.next = newNode;
        length++;
    }

    // 尾插法
    public void tailInsert(int data) {
        DoubleNode newNode = new DoubleNode(data);
        DoubleNode tail = head.prev;
        tail.next = newNode;
        newNode.prev = tail;
        newNode.next = head;
        head.prev = newNode;
        length++;
    }

    // 删除节点
    public boolean deleteNode(int data) {
        DoubleNode current = head.next;
        while (current != head) {
            if (current.data == data) {
                current.prev.next = current.next;
                current.next.prev = current.prev;
                length--;
                return true;
            }
            current = current.next;
        }
        return false;
    }

    // 反转链表
    public void reverseList() {
        if (head.next == head || head.next.next == head) {
            return;
        }
        DoubleNode current = head.next;
        do {
            DoubleNode temp = current.next;
            current.next = current.prev;
            current.prev = temp;
            current = temp;
        } while (current != head);
        // 调整头节点的前后指针
        DoubleNode tail = head.prev;
        head.next = tail;
        tail.prev = head;
    }

    // 打印链表
    public void printList() {
        if (head.next == head) {
            System.out.println("空链表");
            return;
        }
        DoubleNode current = head.next;
        StringBuilder sb = new StringBuilder();
        while (current != head) {
            sb.append(current.data).append(" , ");
            current = current.next;
        }
        if (sb.length() > 2) {
            sb.setLength(sb.length() - 3);
        }
        System.out.println(sb.toString());
    }

    public int getLength() {
        return length;
    }
}

class DoubleCircularLinkedListDemo {
    public static void main(String[] args) {
        DoubleCircularLinkedList list = new DoubleCircularLinkedList();

        System.out.println("----------初始化链表----------");
        // 通过公共方法获取 head 并打印哈希值
        System.out.println("head= " + list.getHead().hashCode());

        System.out.println("----------增加结点(头插法)----------");
        list.headInsert(13);
        list.headInsert(14);
        list.headInsert(15);
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("----------增加结点(尾插法)----------");
        list.tailInsert(21);
        list.tailInsert(22);
        list.tailInsert(23);
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("----------删除结点----------");
        System.out.println("删除数据为5120的结点");
        System.out.println("删除前：");
        list.printList();
        boolean deleteResult = list.deleteNode(5120);
        System.out.println(deleteResult ? "删除成功" : "删除失败");
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("删除数据为13的结点");
        System.out.println("删除前：");
        list.printList();
        deleteResult = list.deleteNode(13);
        System.out.println(deleteResult ? "删除成功" : "删除失败");
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("删除数据为23的结点");
        System.out.println("删除前：");
        list.printList();
        deleteResult = list.deleteNode(23);
        System.out.println(deleteResult ? "删除成功" : "删除失败");
        list.printList();
        System.out.println("length: " + list.getLength());

        System.out.println("----------反转链表----------");
        System.out.println("反转前");
        list.printList();
        list.reverseList();
        System.out.println("反转后");
        list.printList();
    }
}
